var common = require('../../../utils/common.js');
var app = getApp();


Page({
    data: {
        __PUBLIC__:app.__PUBLIC__ ,
        displayFile:false,
    },
    onLoad: function (options) {
        common.bestCurl('GET','/rent/detail',res=>{
            if(res.statusCode == 200){
                if(res.data.code==2){
                    console.log(res.data)
                }else{
                    if(res.data.rent_file.length!=0){
                        for (var index in res.data.rent_file) {
                            let url = res.data.rent_file[index].file_url;
                            let extStart = url.lastIndexOf(".");
                            let ext = url.substring(extStart, url.length).toUpperCase();
                            let pic = [
                                    '.JPG',
                                    '.PNG',
                                    '.JPEG',
                                    '.BMP',
                                    '.GIF',
                                    '.WEBP'
                                ];
                            if(pic.indexOf(ext)!=-1){
                                res.data.rent_file[index].ext = 'pic';
                            }else if(ext=='.DOC'||ext=='.DOCX'){
                                res.data.rent_file[index].ext = 'doc';

                            }else if(ext=='.PDF'){
                                res.data.rent_file[index].ext = 'pdf';
                            }else{
                                console.log('非法文件')
                            }   
                        }
                        this.setData({details:res.data,displayFile:true})
                    }else{
                        this.setData({details:res.data,displayFile:false})
                    }
                }
            }
        },{'id':options.id})
    },
    showFile:function (event) {
        var url = event.currentTarget.dataset.url;
        var fileUrl = app.__UPLOADS__+'/'+url;
        var extStart = url.lastIndexOf(".");
        var ext = url.substring(extStart, url.length).toUpperCase();
        var pic = [
            '.JPG',
            '.PNG',
            '.JPEG',
            '.BMP',
            '.GIF',
            '.WEBP'
        ];
        var fileType = [
            '.DOC',
            '.XLS',
            '.PPT',
            '.PDF',
            '.DOCX',
            '.XLSX',
            '.PPTX'
        ];
        if(pic.indexOf(ext)!=-1){
            wx.previewImage({
                current: '', 
                urls: [
                    fileUrl
                ] 
            })
        }else if(fileType.indexOf(ext)!=-1){
            wx.showLoading({
              title: '加载中',
              mask:true
            });
            wx.downloadFile({
                url: fileUrl, 
                success: function(res) {
                    wx.hideLoading();
                    if (res.statusCode === 200) {
                        wx.openDocument({
                            filePath: res.tempFilePath,
                            success: function (res) {
                                console.log('打开文档成功')
                            }
                        })
                    }
                }
            });
        }else{
            wx.showModal({
                content: '非法文件,禁止预览',
                showCancel: false,
            });        
        }     
    },
    delete:function (event) {
        id = event.currentTarget.id
        wx.showModal({
            title: '确认要删除吗?',
            cancelText: "取消",
            confirmText: "确定",
            success: function (res) {
                if (res.confirm) {
                    common.bestCurl('GET','/rent/delete',res=>{
                        if(res.statusCode == 200){
                            if(res.data.code == 1){
                                wx.showToast({
                                    title: '删除成功',
                                    icon: '',
                                    duration: 2000
                                });
                                setTimeout(res=>{
                                    wx.redirectTo({
                                        url: '../list/list'
                                    })
                                },2000)
                            }else{
                                wx.showToast({
                                    title: '未删除成功,请重试',
                                    icon: '',
                                    duration: 3000
                                });
                            }
                        }
                    },{'id':id})
                }else{
                    console.log('用户点击辅助操作')
                }
            }
        });
    },
    back:function (event){
        id = event.currentTarget.id;
        wx.navigateTo({
            url: '../back/back?id='+id
        })
    },
    edit:function(event){
        id = event.currentTarget.id;
        wx.navigateTo({
            url: '../edit/edit?id='+id
        })
    }
})